#!/bin/bash

# 分布式存储系统 - ChunkServer启动脚本
# 根据EC配置启动多个chunkserver进程

# 配置参数
CHUNKMASTER_PORT=50050
CHUNKSERVER_BASE_PORT=50051
CHUNKSERVER_COUNT=6  # EC 4+2 模式需要6个chunkserver

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}=== 分布式存储系统 - ChunkServer启动脚本 ===${NC}"
echo ""

# 检查chunkmaster是否运行
echo -e "${YELLOW}1. 检查chunkmaster服务状态...${NC}"
if ! nc -z localhost $CHUNKMASTER_PORT 2>/dev/null; then
    echo -e "${RED}错误: chunkmaster服务未运行在端口 $CHUNKMASTER_PORT${NC}"
    echo "请先启动chunkmaster服务:"
    echo "  cd persistent_layer/chunkmaster/build && ./chunkmaster"
    exit 1
else
    echo -e "${GREEN}✓ chunkmaster服务运行正常${NC}"
fi

# 检查chunkserver可执行文件
echo -e "${YELLOW}2. 检查chunkserver可执行文件...${NC}"
CHUNKSERVER_BIN="persistent_layer/chunkserver/build/chunkserver_server"
if [ ! -f "$CHUNKSERVER_BIN" ]; then
    echo -e "${RED}错误: 找不到chunkserver可执行文件: $CHUNKSERVER_BIN${NC}"
    echo "请先编译chunkserver:"
    echo "  cd persistent_layer/chunkserver/build && make"
    exit 1
else
    echo -e "${GREEN}✓ chunkserver可执行文件存在${NC}"
fi

# 停止已存在的chunkserver进程
echo -e "${YELLOW}3. 停止已存在的chunkserver进程...${NC}"
pkill -f "chunkserver_server" 2>/dev/null
sleep 2

# 启动多个chunkserver进程
echo -e "${YELLOW}4. 启动 $CHUNKSERVER_COUNT 个chunkserver进程...${NC}"
CHUNKSERVER_PIDS=()

for ((i=0; i<CHUNKSERVER_COUNT; i++)); do
    PORT=$((CHUNKSERVER_BASE_PORT + i))
    echo -e "${BLUE}启动 chunkserver_$i 在端口 $PORT...${NC}"
    
    # 创建日志目录
    mkdir -p logs
    
    # 启动chunkserver进程
    $CHUNKSERVER_BIN $PORT > "logs/chunkserver_$i.log" 2>&1 &
    PID=$!
    CHUNKSERVER_PIDS+=($PID)
    
    # 等待进程启动
    sleep 1
    
    # 检查进程是否启动成功
    if kill -0 $PID 2>/dev/null; then
        echo -e "${GREEN}✓ chunkserver_$i (PID: $PID) 启动成功，端口: $PORT${NC}"
    else
        echo -e "${RED}✗ chunkserver_$i 启动失败${NC}"
        exit 1
    fi
done

echo ""
echo -e "${GREEN}=== 所有chunkserver进程启动完成 ===${NC}"
echo "进程信息:"
for ((i=0; i<CHUNKSERVER_COUNT; i++)); do
    PORT=$((CHUNKSERVER_BASE_PORT + i))
    PID=${CHUNKSERVER_PIDS[$i]}
    echo -e "  chunkserver_$i: PID=$PID, 端口=$PORT, 日志=logs/chunkserver_$i.log"
done

echo ""
echo -e "${YELLOW}=== 系统状态 ===${NC}"
echo "EC配置: 4+2 (数据块:4, 校验块:2)"
echo "ChunkServer数量: $CHUNKSERVER_COUNT"
echo "端口范围: $CHUNKSERVER_BASE_PORT - $((CHUNKSERVER_BASE_PORT + CHUNKSERVER_COUNT - 1))"
echo ""

echo -e "${YELLOW}=== 使用说明 ===${NC}"
echo "1. 启动chunkmaster: cd persistent_layer/chunkmaster/build && ./chunkmaster"
echo "2. 启动chunkserver_client: cd persistent_layer/chunkserver/build && ./chunkserver_client"
echo "3. 查看日志: tail -f logs/chunkserver_*.log"
echo "4. 停止所有服务: ./stop_chunkservers.sh"
echo ""

echo -e "${BLUE}按 Ctrl+C 停止所有chunkserver进程...${NC}"

# 信号处理函数
cleanup() {
    echo ""
    echo -e "${YELLOW}正在停止所有chunkserver进程...${NC}"
    for PID in "${CHUNKSERVER_PIDS[@]}"; do
        if kill -0 $PID 2>/dev/null; then
            echo -e "${BLUE}停止进程 $PID...${NC}"
            kill $PID
        fi
    done
    
    # 等待进程结束
    sleep 2
    
    # 强制杀死残留进程
    pkill -f "chunkserver_server" 2>/dev/null
    
    echo -e "${GREEN}所有chunkserver进程已停止${NC}"
    exit 0
}

# 设置信号处理
trap cleanup SIGINT SIGTERM

# 等待用户中断
while true; do
    sleep 1
done 